<html>
<head><meta charset="utf-8"><title>Trying to tackle bindings_afyer_at issue · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html">Trying to tackle bindings_afyer_at issue</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215210625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Trying%20to%20tackle%20bindings_afyer_at%20issue/near/215210625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html#215210625">(Oct 31 2020 at 20:29)</a>:</h4>
<p>I've been trying to tackle to tackle <a href="https://github.com/rust-lang/rust/issues/69971">https://github.com/rust-lang/rust/issues/69971</a>.</p>
<p>Right now, the bindings in <code>@</code> pat occur from right to left. This make </p>
<div class="codehilite"><pre><span></span><code>    let y @ NonCopyStruct { copy_field: z } = x;
</code></pre></div>
<p>into</p>
<div class="codehilite"><pre><span></span><code>    let y = x;
    let NonCopyStruct { copy_field: z } = x;
</code></pre></div>
<p>I've fixed this by reversing the binding order for matches. While this seems to fix the issue, my question is whether this is a valid fix (as in this does not go against the language semantics)?</p>



<a name="215216189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Trying%20to%20tackle%20bindings_afyer_at%20issue/near/215216189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html#215216189">(Oct 31 2020 at 23:08)</a>:</h4>
<p>But what about <code>y @ ... { cell_field: ref z }</code>.<br>
I personally think that these cases could have a lint attached to express which interpretation the compiler went for, but for sure which direction is "natural" is far from obvious (for instance , it feels more natural to me that <code>y</code> be bound first, moving out of <code>x</code>, and that <code>z</code>, be it by copy, in your example, or by ref, in mine, should be matched against the field of <code>y</code>. That's the only way I could see <code>ref mut z</code> working, for instance).</p>



<a name="215229938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Trying%20to%20tackle%20bindings_afyer_at%20issue/near/215229938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html#215229938">(Nov 01 2020 at 06:19)</a>:</h4>
<p><span class="user-mention" data-user-id="232018">@Daniel Henry-Mantilla</span> What exactly about <code>y @ ... { cell_field: ref z }</code> is problematic? I didn't quite get it.</p>
<p>Also, I just want to point out that this kind of binding was possible before this feature was removed (pre 1.0). So if anyone is familiar with how the semantics were worked out in those days, that would be great.</p>



<a name="215255391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Trying%20to%20tackle%20bindings_afyer_at%20issue/near/215255391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html#215255391">(Nov 01 2020 at 18:19)</a>:</h4>
<p>Well, I realize that <code>Cell</code> not being <code>Copy</code> avoids the ambiguity of my example, but assuming it were (which it definitely could be in the future), then depending on the order of pattern bindings being applied, <code>z</code> may be a mutable reference to the matchee, <code>x</code>, before it gets copied into <code>y</code>, or a mutable reference to the <code>y</code> copy.</p>
<p>An example (in a world where <code>Cell&lt;impl Copy&gt; : Copy</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Cell</span>::<span class="n">new</span><span class="p">(</span><span class="mi">42</span><span class="p">),</span><span class="w"> </span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">at_ft</span><span class="p">,</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// `y` becomes a `Copy` of `x`.</span>
<span class="o">*</span><span class="n">at_ft</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">x</span><span class="p">.</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">.</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// (one of them will be `42`, the question is which).</span>
</code></pre></div>
<p>My personal view is that in this example <code>y</code> should be the one having been mutated, since it makes sense to deconstruct "starting from the outside", and that <code>at_ft</code> is thus <code>&amp;Cell</code>-borrowing a field of <code>y</code>. But such view may not be obvious to everybody (or even to all situations).</p>



<a name="215255981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Trying%20to%20tackle%20bindings_afyer_at%20issue/near/215255981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html#215255981">(Nov 01 2020 at 18:33)</a>:</h4>
<p>Hmm. I just encountered this while updating the tests. I think I'll open the PR and we can discuss on there</p>



<a name="215256211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Trying%20to%20tackle%20bindings_afyer_at%20issue/near/215256211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Trying.20to.20tackle.20bindings_afyer_at.20issue.html#215256211">(Nov 01 2020 at 18:39)</a>:</h4>
<p>I think in the same vein, the following is ambiguous too, but it compiles</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(bindings_after_at)]</span><span class="w"></span>

<span class="cp">#[derive(Copy, Clone)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">NonCopyStruct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">copy_field</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">NonCopyStruct</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">NonCopyStruct</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">copy_field</span>: <span class="nc">z</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>